<template>
<div class="container content-chat">
    <!-- py-4上间隔 -->
    <div>
      <div class="row mb-3">
        <div class="col-9 themed-grid-col clearmp">
          <div class="content-top">
            <!-- 消息区 -->
            <div class="lite-chatbox">
              <div class="cleft cmsg">
                <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;" :src="'../../lib/chat/img/B.jpg'"/>
                <span class="name"><span class="htitle admin">admin</span>SuperPaxxs</span>
                <span class="content">欢迎光临豪车之家官方旗舰店，请问您有什么要咨询的吗？</span>
              </div>
            </div>
            <div class="lite-chatbox" v-show="right1">
              <div class="cright cmsg">
                <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'../../lib/chat/img/B.jpg'"/>
                <span class="name">用户{{mId}}</span>
                <span class="content">{{mysend}}</span>
              </div>
            </div>
            <div class="lite-chatbox" v-show="left" id="left">
              <div class="cleft cmsg">
                <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'../../lib/chat/img/A.jpg'"/><!-- ../../lib/chat/img/B.jpg -->
                <span class="name"><span class="htitle admin">admin</span>SuperPaxxs</span>
                <span class="content" id="amess"></span>
              </div>
            </div>
            <div class="lite-chatbox" v-show="right2" id="right2">
              <div class="cright cmsg">
                <img class="headIcon radius" ondragstart="return false;"  oncontextmenu="return false;"  :src="'../../lib/chat/img/B.jpg'"/>
                <span class="name">用户{{mId}}</span>
                <span class="content">{{mysend}}</span>
              </div>
            </div>
            <!-- <div v-for="(c,i) in charmessage" :key="i" v-html="c"></div> -->
          </div>
          <div class="content-bottom">
            <textarea class="message" id="message" cols="30" rows="10" placeholder="在此输入文本消息..." v-model="message" @keyup.enter="sendMessage"></textarea>
          </div>
          <button class="btn btn-primary" id="send" @click="sendMessage">发送</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return {
      webs:"",
      mId:1,
      message:"",
      info:{},
      mysend:"",
      // 聊天气泡
      right1:false,
      left:false,
      right2:false,
      account:0
    }
  },
  methods:{
    localSocket() {
      let that = this;
      if ("WebSocket" in window) {
          // console.log("您的浏览器支持 WebSocket!");
          // that.ws = new WebSocket("ws://chatserver.cn.utools.club/koa/ws?id="+this.mId);
          that.ws = new WebSocket("ws://127.0.0.1/koa/ws?id="+this.mId);
          that.global.setWs(that.ws);
          that.ws.onopen = function () {
              console.log('websocket连接成功');
          };
          that.ws.onmessage = function (evt) {
            console.log(evt);
            // console.log('连接==>'+evt.data);
          }
          // that.ws.onclose = function () {
          //     // 关闭 websocket
          //     console.log("连接已关闭...");
          //     //断线重新连接
          //     setTimeout(() => {
          //         that.localSocket();
          //     }, 2000);
          // };
      } else {
          // 浏览器不支持 WebSocket
          console.log("您的浏览器不支持 WebSocket!");
          this.openNotificationWithIcon('error', '浏览器', '您的浏览器不支持显示消息请更换', 1,1)
      }
    },
    sendMessage(){
      if(this.account==0){
        this.right1=true;
        this.account++;
      }
      let data = {
        userId: this.mId,
        uId: 5,
        content: this.message
      }
      this.ws.send(JSON.stringify(data));
      this.mysend = this.message;
      this.ws.onmessage = function (evt) {
        // console.log(evt);
        let info = JSON.parse(evt.data);
        if(info.isTrusted){
          console.log(document.getElementById("amess").innerHTML = info.content);
          // document.getElementById("amess").innerHTML = info.content
        }else{
          console.log(evt.data);
          console.log(document.getElementById("amess").innerHTML = info.content);
          document.getElementById("left").style.display ="block";
          // this.left = true;
        }
        // console.log(document.getElementById("amess").innerHTML = info.content);
        // console.log(evt.data);
      }
      this.message="";
    },
  },
  created(){
    this.localSocket();
  },
  beforeDestroy(){
    this.ws.onclose = function(){
      // 关闭 websocket
      alert("连接已关闭..."); 
    };
  }
}
</script>

<style scoped>
@import url("../../lib/chat/litewebchat.min.css");

.content-chat{
  height: 100%;
  padding: 0;
  margin: 0;
}
.chat-top{
  margin-bottom: 0px;
}
.content-top{
  height: 400px;
  min-height:400px;
}
.content-bottom{
  border-top: 1px solid rgba(86, 61, 124, .2);
  height: 150px;
  min-height:150px;
}
.question{
  height: 50px;
  border-bottom: 1px solid rgba(86, 61, 124, .2);
  line-height: 50px;
  padding-left: 20px;
}
.clearmp{
  margin: 0;
  padding: 0;
}
.message{
  width: 100%;
  height: 95%;
  border: none;
  resize: none;
  padding: 10px 5px;
  background-color: #E6E2EB;
}
.message:focus{
  outline: none;
}
#send{
  float: right;
}
</style>